package com.ruoyi.qywx.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.ruoyi.common.utils.CollectionUtils;
import com.ruoyi.common.utils.NumberUtils;
import com.ruoyi.common.utils.StrUtils;
import com.ruoyi.qywx.entity.ContactEntity;
import com.ruoyi.qywx.entity.Dept;
import com.ruoyi.qywx.mapper.ContactMapper;
import com.ruoyi.qywx.model.vo.ContactVo;
import com.ruoyi.qywx.model.vo.DeptVo;
import com.ruoyi.qywx.service.inters.ContactService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author : hcl
 * @Date : 2023/8/9 17:01
 */
@Service
public class ContactServiceImpl implements ContactService {
    @Resource
    private ContactMapper contactMapper;

    @Override
    public List<Dept> selectTeamDept(Long teamId) {
        return contactMapper.selectTeamDept(null, teamId);
    }

    @Override
    public List<DeptVo> deptList(Long deptId, Long teamId) {
        List<Dept> depts = contactMapper.selectTeamDept(deptId, teamId);
        if (CollectionUtils.isEmpty(depts)) return Collections.emptyList();
        return depts.stream().map(d -> {
            DeptVo vo = new DeptVo();
            vo.setName(d.getName());
            vo.setParentId(d.getParentId());
            vo.setOrder(d.getOrder());
            vo.setId(d.getDeptId());
            return vo;
        }).collect(Collectors.toList());
    }

    @Override
    public List<ContactVo> contactList(Long deptId, String name, Long teamId) {
        QueryWrapper<ContactEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("team_id", teamId);
        if (NumberUtils.isTrue(deptId)) {
            wrapper.eq("dept_id", deptId);
        }
        if (StrUtils.isNotEmpty(name)) {
            wrapper.eq("name", name);
        }
        List<ContactEntity> list = contactMapper.selectList(wrapper);
        List<Dept> depts = selectTeamDept(teamId);
        Map<Long, Dept> deptMap = depts.stream().collect(Collectors.toMap(Dept::getDeptId, d -> d));
        if (CollectionUtils.isEmpty(list)) return Collections.emptyList();
        return list.stream().map(en -> {
            ContactVo vo = new ContactVo(en.getUserId(), en.getName());
            if (StrUtils.isNotEmpty(en.getDepartments())) {
                List<DeptVo> collect = CollectionUtils.toLongList(en.getDepartments())
                        .stream()
                        .map(id -> {
                            Dept dept = deptMap.get(id);
                            DeptVo dvo = new DeptVo();
                            dvo.setId(id);
                            dvo.setName(dept.getName());
                            dvo.setOrder(dept.getOrder());
                            dvo.setParentId(dept.getParentId());
                            return dvo;
                        })
                        .collect(Collectors.toList());
                vo.setDepts(collect);
            }
            return vo;
        }).collect(Collectors.toList());
    }

    @Override
    public List<ContactEntity> selectContactByIds(Collection<String> userIds, Long teamId) {
        if (CollectionUtils.isNotEmpty(userIds)) {
            QueryWrapper<ContactEntity> wrapper = new QueryWrapper<>();
            wrapper.select("user_id", "name", "avatar")
                    .in("user_id", userIds)
                    .eq("team_id", teamId);
            return contactMapper.selectList(wrapper);
        }
        return Collections.emptyList();
    }

    /**
     * @param userIds 企业微信userIds
     * @return map: userId: 姓名
     */
    @Override
    public Map<String, String> contactNamesMap(Collection<String> userIds, Long teamId) {
        List<ContactEntity> contacts = selectContactByIds(userIds, teamId);
        if (CollectionUtils.isNotEmpty(contacts)) {
            return contacts.stream().collect(Collectors.toMap(ContactEntity::getUserId, ContactEntity::getName));
        }
        return Collections.emptyMap();
    }

    @Transactional
    @Override
    public void createUsers(List<ContactEntity> contactEntities) {
        contactMapper.insertBatchSomeColumn(contactEntities);
    }

    @Transactional
    @Override
    public void updateUser(ContactEntity contact) {
        UpdateWrapper<ContactEntity> wrapper = new UpdateWrapper<>();
        wrapper.eq("team_id", contact.getTeamId())
                .eq("user_id", contact.getUserId());
        contactMapper.update(contact, wrapper);
    }

    @Transactional
    @Override
    public void createUser(ContactEntity contactEntity) {
        contactMapper.insert(contactEntity);
    }

    @Transactional
    @Override
    public void deleteUser(String userId, Long teamId) {
        if (!NumberUtils.isTrue(teamId)) {
            return;
        }
        QueryWrapper<ContactEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("team_id", teamId);
        if (StrUtils.isNotEmpty(userId)) {
            wrapper.eq("user_id", userId);
        }
        contactMapper.delete(wrapper);
    }
}
